<?php
class CoachAction extends Action{
	//显示教练列表
	public function showCoach(){
		$lat = $_REQUEST["lat"];
		$lng = $_REQUEST["lng"];
		
		$uid = $_REQUEST["uid"];
		//分页设置
		$pagecount=isset($_REQUEST['pagecount'])?$_REQUEST['pagecount']:"10";//每页显示数量
		$pagenum=$pagecount*((isset($_REQUEST['pagenum'])?$_REQUEST['pagenum']:"1")-1);//当前页数
	
		$sql ="select c.*,u.avatar,(2 * 6378.137* ASIN(SQRT(POW(SIN(PI()*($lat-u.lat)/360),2)+COS(PI()*$lng/180)* COS(lat * PI()/180)*POW(SIN(PI()*($lng-u.lng)/360),2)))) as distance from yjcoach as c left join yjuser as u on u.uid = c.uid where u.is_coach = 1 order by distance limit $pagenum,$pagecount";
		$data = M("User")->query($sql);
		$user = New UserAction();
		if ($data == false){
			$value = ["code" =>"wrong"];
			header("Content-Type: text/json");
			die(json_encode($value));
		}else{
			for($i=0;$i<count($data);$i++){
				$image = D('attach')->where('attach_id='.$data[$i]['photo'])->find();
				$image_url = $image['save_path'].$image['save_name'];
				$data[$i]['photo'] = getImageUrl($image_url);
					
				$image = D('attach')->where('attach_id='.$data[$i]['diploma_photo'])->find();
				$image_url = $image['save_path'].$image['save_name'];
				$data[$i]['diploma_photo'] = getImageUrl($image_url);
				if(isset($uid)){
					$data[$i]["is_follow"] = $user->isFollow($uid, $data[$i]["uid"])?1:0;
				}
			}
			
			$value = ["code" =>"success","data"=>$data];
			header("Content-Type: text/json");
			die(json_encode($value));
		}
	}
	public function showOneCoach(){
		$coach_id = $_REQUEST["coach_id"];
		$uid  = $_REQUEST["uid"];
		
		$sql ="select u.avatar,u.heigh,u.weight,u.email,c.* from yjcoach as c left join yjuser as u on u.uid = c.uid where c.uid = $coach_id "; 
		if ($data=M("Coach")->query($sql)){
			$user = New UserAction();
			for($i=0;$i<count($data);$i++){
				$image = D('attach')->where('attach_id='.$data[$i]['photo'])->find();
				$image_url = $image['save_path'].$image['save_name'];
				$data[$i]['photo'] = getImageUrl($image_url);
					
				$image = D('attach')->where('attach_id='.$data[$i]['diploma_photo'])->find();
				$image_url = $image['save_path'].$image['save_name'];
				$data[$i]['diploma_photo'] = getImageUrl($image_url);
				
				if(isset($uid)){
					$data[$i]["is_follow"] = $user->isFollow($uid, $coach_id)?1:0;
				}
			}
			if(count($data)<1 || $data == false){
				$data = [];
			}

			
			$value = ["code" =>"success","data"=>$data];
			header("Content-Type: text/json");
			die(json_encode($value));
		}else{
			$value = ["code" =>"wrong","massage"=>M("Coach")->getDbError()];
			header("Content-Type: text/json");
			die(json_encode($value));
		}
		
	}
	//显示教练的课程
	public function showCoachClass(){
		$coach_id = $_REQUEST["coach_id"];
		$data = M("Coach_order")->where("coach_id = $coach_id")->select();
		if(count($data)<1 || $data == false){
			$data = [];
		}
		$value = ["code" =>"success","data"=>$data];
		header("Content-Type: text/json");
		die(json_encode($value));
	}
	
	//约教练
	public function orderCoach(){
		$coach = M("Coach_order_data");
		$uid = $_REQUEST["uid"];
		$coach_id = $_REQUEST["coach_id"];
		$oid = $_REQUEST["oid"];
		if ($uid == $coach_id){
			$value = ["code" =>"wrong","massage"=> "不可以约自己"];
			header("Content-Type: text/json");
			die(json_encode($value));
		}
		$class = M("Coach_order")->where("oid = $oid")->find();
		if($class["people_curren"] >= $class["people_limit"]){
			$value = ["code" =>"full","massage"=> "人数已满。"];
			header("Content-Type: text/json");
			die(json_encode($value));
		}
		
		$class_data = M("Coach_order_data")->where("oid = $oid and uid = $uid")->find();
		if(isset($class_data["id"])){
			$value = ["code" =>"already","massage"=> "已经参加过了。"];
			header("Content-Type: text/json");
			die(json_encode($value));
		}
		$coach->uid = $uid;
		$coach->oid = $oid;
		$coach->join_time = time();
		if ($coach->add()){
			M("Coach_data")->where("oid = $oid")->setInc("people_curren",1);
			
			$value = ["code" =>"success"];
			header("Content-Type: text/json");
			die(json_encode($value));
		}else{
			$value = ["code" =>"wrong"];
			header("Content-Type: text/json");
			die(json_encode($value));
		}

	}
	//显示教练的约单
	public function showCoachOrder(){
		$uid = $_REQUEST["uid"];
		$status = $_REQUEST["status"];
		$data = M("Coach_order")->where("coach_id = $uid and status = $status ")->select();
		if ($data){
			$value = ["code" =>"success","data"=>$data];
			header("Content-Type: text/json");
			die(json_encode($value));
		}else{
			if(mysql_num_rows($data) < 1){
				$value = ["code" =>"success","data"=>null];
				header("Content-Type: text/json");
				die(json_encode($value));
			}
			$value = ["code" =>"wrong","massage"=>M("Coach_order")->getDbError(),"as"=>is_array($data)];
			header("Content-Type: text/json");
			die(json_encode($value));
		}
	}
	//申请教练
	public function applyCoach(){
		$coach = M("Coach");
		//获取申请资料
		$coach->uid = $_REQUEST["uid"];
		$coach->level = $_REQUEST["level"];
		$coach->realname = $_REQUEST["realname"];
		$coach->sex = $_REQUEST["sex"];
		$coach->birth = $_REQUEST["birth"];
		$coach->sport = $_REQUEST["sport"];
		$coach->apply_status = 0;
		$coach->apply_time = time();
		
		if(isset($_FILES["photo"])&&isset($_FILES["diploma_photo"])){
			if($_FILES['photo']['error'] == 0&&$_FILES['diploma_photo']['error'] == 0){
					
				$info=$this->uploadImage();
				
				if(!$info){
					$value = ['code'=>'wrong'];
					header("Content-Type: text/json");
					die(json_encode($value));
				}
			
				$attach = model('Attach');
				$option = array(
						'save_to_db'=>1,
						'custom_path'=>'user/coach/'.date("Ymd").'/',
				);
				
				$info = $attach->saveInfo($info,$option);
				
				for ($i=0;$i<count($info);$i++){
					if($info[$i]["key"] == "photo"){
						$coach->photo =$info[$i]["attach_id"];
					}
					if($info[$i]["key"] == "diploma_photo"){
						$coach->diploma_photo = $info[$i]["attach_id"];
					}
				}
				
			}else{
				//其中一张有错误
				$value = ["code" =>"wrong","massage"=>"图片有错误"];
				header("Content-Type: text/json");
				die(json_encode($value));
			}
			
		}else{
			//没有上传两张
			$value = ["code" =>"wrong","massage"=>"没有两张图片"];
			header("Content-Type: text/json");
			die(json_encode($value));
			
		}
		
		if ($coach->add()){
			$value = ["code" =>"success"];
			header("Content-Type: text/json");
			die(json_encode($value));
		}else{
			$value = ["code" =>"wrong","massage"=>"添加失败"];
			header("Content-Type: text/json");
			die(json_encode($value));
		}
	}
	
	//完成约单
	public function finishCoachOrder(){
		$upload_info = $this->uploadImage();
		if(!$upload_info){
			$value = ['code'=>'wrong'];
			header("Content-Type: text/json");
			die(json_encode($value));
		}
	}
	
	public function uploadImage(){//【上传动态附带图片】
	
		$upload	= new UploadFile();
	
		$upload= new UploadFile();
		$upload->autoSub=true; //开启子目录保存文件
		$upload->maxSize = 2097152;//最大为2m
		$upload->exts=array('jpg','jpeg','gif','png','bmp');//可上传的类型
		$upload->savePath='data/upload/user/coach/'.date("Ymd").'/';
	
		$info = $upload->upload();
	
		if($info){
			$result= $upload->getUploadFileInfo();
				
		}else{
				
			return  false;
		}
	
	
		return $result;
	
	}
	public function showUserClass(){
		$uid = t($_REQUEST["uid"]);
		$status = t($_REQUEST["status"]);//状态 0未完成 1已完成2？？
		$sql = "select * from yjcoach_order_data as d left join yjcoach_order as o on d.oid = o.oid where uid = $uid and o.status = $status order by join_time desc";
		$data = M("Coach_order_data")->query($sql);
		if ($data != false){
			for($i=0;$i<count($data);$i++){
				$data[$i]["coach_name"] = M("Coach")->where("uid = ".$data[$i]["coach_id"])->find()["realname"];
			}
			$value=["code"=>"success","data"=>$data];
		}else{
			if(mysql_affected_rows($data)){
				$value=["code"=>"success","data"=>[]];
			}else{
			$value=["code"=>"wrong","data"=>M("Coach_order_data")->getDbError()];
			}
		}
		header("Content-Type: text/json");
		die(json_encode($value));
	}
	/**
	 * 教练状态
	 *  0 申请中
	 *  1审核通过
	 *  -1 被禁用
	 *  -2 不通过
	 */
	public function isApplyCoach(){
		$uid = trim($_REQUEST["uid"]);
		$coach = M("Coach");
		$data = $coach->where("uid = $uid")->find();
		if(isset($data["id"])){
			if($data["apply_status"] == 1){
				$value = ["code"=>"wrong","massage"=>"isCoach"];
			}
			if($data["apply_status"] == 0){
				$value = ["code"=>"wrong","massage"=>"appling"];
			}
			if($data["apply_status"] == -1){
				$value = ["code"=>"success","massage"=>"no pass"];
			}
			if($data["apply_status"] == -1){
				$value = ["code"=>"wrong","massage"=>"band"];
			}
		}else{
			$value = ["code"=>"success","massage"=>"ok"];
		}
		header("Content-Type: text/json");
		die(json_encode($value));
	}

}